package org.itzheng.yuezhi.real.main;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import org.itzheng.yuezhi.R;
import org.itzheng.yuezhi.adapter.BaseFragmentPagerAdapter;
import org.itzheng.yuezhi.base.BaseActivity;

import java.util.ArrayList;

/**
 * Title:<br>
 * Description: <br>
 * Company: <br>
 *
 * @author ZhengYongdong
 * @email ItZheng@ZoHo.com
 * @date 2016/6/24 0024
 */
public class ContentActivity extends BaseActivity {
    ViewPager vp_main_content;
    RadioGroup rg_main_radio_group;
    RadioButton rb_main_button_01;
    RadioButton rb_main_button_02;
    RadioButton rb_main_button_03;
    RadioButton rb_main_button_04;
    ArrayList<Fragment> fragments;
    String[] group = {"首页", "阅读", "探索", "工具"};

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_content);
        initData();
        initView();
    }

    private void initData() {
        fragments = new ArrayList<>();
        fragments.add(new HomeFragment());
        fragments.add(new FindFragment.ReadFragment());
        fragments.add(new FindFragment());
        fragments.add(new ToolsFragment());
    }

    private void initView() {
        //
        vp_main_content = (ViewPager) findViewById(R.id.vp_main_content);
        setContentViewpager();
        //找到底部的按钮控件
        rg_main_radio_group = (RadioGroup) findViewById(R.id.rg_main_radio_group);
        rb_main_button_01 = (RadioButton) findViewById(R.id.rb_main_button_01);
        rb_main_button_02 = (RadioButton) findViewById(R.id.rb_main_button_02);
        rb_main_button_03 = (RadioButton) findViewById(R.id.rb_main_button_03);
        rb_main_button_04 = (RadioButton) findViewById(R.id.rb_main_button_04);
        setBottomButton();

    }

    /**
     * 设置底部按钮点击事件
     */
    private void setBottomButton() {
        rb_main_button_01.setText(group[0]);
        rb_main_button_02.setText(group[1]);
        rb_main_button_03.setText(group[2]);
        rb_main_button_04.setText(group[3]);
        //
        rg_main_radio_group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.rb_main_button_01:
                        vp_main_content.setCurrentItem(0);
                        break;
                    case R.id.rb_main_button_02:
                        vp_main_content.setCurrentItem(1);
                        break;
                    case R.id.rb_main_button_03:
                        vp_main_content.setCurrentItem(2);
                        break;
                    case R.id.rb_main_button_04:
                        vp_main_content.setCurrentItem(3);
                        break;
                }
            }
        });

        rb_main_button_01.setChecked(true);
    }

    /**
     * 设置内容
     */
    private void setContentViewpager() {
        vp_main_content.setAdapter(new BaseFragmentPagerAdapter(getSupportFragmentManager(), fragments));
        vp_main_content.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                switch (position) {
                    case 0:
                        rb_main_button_01.setChecked(true);
                        break;
                    case 1:
                        rb_main_button_02.setChecked(true);
                        break;
                    case 2:
                        rb_main_button_03.setChecked(true);
                        break;
                    case 3:
                        rb_main_button_04.setChecked(true);
                        break;
                }

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }
}
